import { CommonObj } from "@/types";
import { getMockList } from "./_utils";
import { getDictLabel, getDictMap, getDictValues } from "../dict";
import Mock from "mockjs";
import dayjs from "dayjs";
import allSchool from "../data/school";
import allCompany from "../data/company";
import allJob from "../data/job";
import allRegions from "../data/regions";
import allDepartment from "../data/department";
import allNavs from "../data/navs";
import { getNavsTree } from "../utils";

const { Random } = Mock;

// 各个角色的账号数量占比
const roleWeight: CommonObj = {
  2: { phonePre: "18483221518", amount: 1 },
  1: { phonePre: "180", amount: 2 },
  0: { phonePre: "181", amount: 3 },
  // 3: { phonePre: "182", amount: 4 },
  // 4: { phonePre: "183", amount: 5 },
  // 5: { phonePre: "184", amount: 6 },
  // 6: { phonePre: "185", amount: 10 },
};

export const defaultDesignMap = {
  common: {
    name: "通用",
    getList() {
      const roles = getDictValues("D_RoleType");
      const allJobNames = allJob.map((it) => it.name);
      const allSchoolNames = allSchool.map((it) => it.name);
      const allCompanyNames = allCompany.map((it) => it.full_name);
      const allIncomeTypes = [
        "小于10w",
        "10~20w",
        "20~30w",
        "30~50w",
        "100w以上",
      ];
      const roleMap: CommonObj = getDictMap("D_RoleType");
      let untilInd = 0;
      let roleInd = 0;
      function getInfo(ind: number) {
        if (ind === untilInd) {
          untilInd += roleWeight[roleInd].amount;
          roleInd++;
          return true;
        }
      }
      return getMockList(
        [
          // 等价： id:id:自增id
          // 含义： 字段模板名称:字段属性名:字段备注
          "id",
          "userCode",
          "userName:name:用户姓名",
          "gender",
          "nickname",
          "avatar",
          "birthday",
          "region:address:现处地址",
          {
            type: "cascader",
            prop: "live_city",
            remark: "现居地",
            attrs: { name: "C_Region", level: 2 },
          },
          {
            type: "cascader",
            prop: "city",
            remark: "家乡",
            attrs: { name: "C_Region", level: 2 },
          },
          {
            type: "dict",
            prop: "role", // 角色类型
            attrs: { name: "D_RoleType" },
            handler({ context }: CommonObj) {
              const { id } = context.currentContext;
              if (roleInd < roles.length) getInfo(id - 1);
              return roleInd - 1;
            },
          },
          {
            type: "phone",
            prop: "phone",
            handler: ({ context }: CommonObj) =>
              Random.phone(roleWeight[context.currentContext.role].phonePre),
          },
          {
            // 账号
            account({ context }: CommonObj) {
              const { id, role } = context.currentContext;
              return `${roleMap[role]}_${id}`;
            },
          },
          {
            password: ({ context }: CommonObj) =>
              `role${context.currentContext.role}123456`,
          },
          {
            age: ({ context }: CommonObj) =>
              dayjs().diff(context.currentContext.birthday, "year"),
          },
          {
            type: "dict",
            prop: "account_status",
            attrs: { name: "D_AccountStatus" },
          }, // 账号状态
          {
            type: "dict",
            prop: "company_status",
            attrs: { name: "D_YesNoStatus" },
          }, // 公司是否认证
          {
            type: "dict",
            prop: "school_status",
            attrs: { name: "D_YesNoStatus" },
          }, // 学校是否认证
          {
            type: "dict",
            prop: "school_certificate_level",
            attrs: { name: "D_EducationType" },
          }, // 学历：专科、本科、硕士、博士
          {
            type: "dict",
            prop: "single_type",
            attrs: { name: "D_MatrimonyStatus" },
          }, // 单身状态
          {
            type: "helper",
            prop: "job_name",
            attrs: { typeKey: "pick", arr: allJobNames },
          }, // 职业名称
          {
            type: "helper",
            prop: "school_name",
            attrs: { typeKey: "pick", arr: allSchoolNames },
          }, // 学校名称
          {
            type: "helper",
            prop: "company_name",
            attrs: { typeKey: "pick", arr: allCompanyNames },
          }, // 公司名称
          {
            type: "helper",
            prop: "income_type_name",
            attrs: { typeKey: "pick", arr: allIncomeTypes },
          }, // 收入类型
          {
            type: "string",
            prop: "produce",
            attrs: { typeKey: "ctitle", min: 3, max: 200 },
          },
          {
            user_data: ({ context }: CommonObj) =>
              JSON.parse(JSON.stringify(context.currentContext)),
          },
          "enableStatus", // 启用禁用状态
          "createUpdate",
          "remark",
        ],
        22
      );
    },
  },
  // 用户表
  user: {
    name: "用户",
    privateKeys: ["password"],
    getList() {
      const roles = getDictValues("D_RoleType");
      const allJobNames = allJob.map((it) => it.name);
      const allSchoolNames = allSchool.map((it) => it.name);
      const allCompanyNames = allCompany.map((it) => it.full_name);
      const allIncomeTypes = [
        "小于10w",
        "10~20w",
        "20~30w",
        "30~50w",
        "100w以上",
      ];
      const roleMap: CommonObj = getDictMap("D_RoleType");
      let untilInd = 0;
      let roleInd = 0;
      function getInfo(ind: number) {
        if (ind === untilInd) {
          untilInd += roleWeight[roleInd].amount;
          roleInd++;
          return true;
        }
      }
      return getMockList(
        [
          "id",
          "userCode",
          "userName:name:用户姓名",
          "gender",
          "nickname",
          "avatar",
          "birthday",
          "region:address:现处地址",
          {
            type: "cascader",
            prop: "live_city",
            remark: "现居地",
            attrs: { name: "C_Region", level: 2 },
          },
          {
            type: "cascader",
            prop: "city",
            remark: "家乡",
            attrs: { name: "C_Region", level: 2 },
          },
          {
            type: "dict",
            prop: "role", // 角色类型
            attrs: { name: "D_RoleType" },
            handler({ context }: CommonObj) {
              const { id } = context.currentContext;
              if (roleInd < roles.length) getInfo(id - 1);
              return roleInd - 1;
            },
          },
          {
            type: "phone",
            prop: "phone",
            handler: ({ context }: CommonObj) =>
              Random.phone(roleWeight[context.currentContext.role].phonePre),
          },
          {
            // 账号
            account({ context }: CommonObj) {
              const { id, role } = context.currentContext;
              return `${roleMap[role]}_${id}`;
            },
          },
          {
            // 剩余AI使用次数
            remain_count() {
              // const { id, role } = context.currentContext;
              return `30`;
            },
          },
          {
            password: ({ context }: CommonObj) =>
              `role${context.currentContext.role}123456`,
          },
          {
            age: ({ context }: CommonObj) =>
              dayjs().diff(context.currentContext.birthday, "year"),
          },
          { type: "dict", prop: "vip_expired", attrs: { name: "D_Expired" } }, // 会员期限
          {
            // vip等级
            vip_level({ context }: CommonObj) {
              const { vip_expired, role } = context.currentContext;
              return vip_expired ? role : 0;
            },
          },
          // 是否关注
          {
            type: "dict",
            prop: "is_focus",
            attrs: { name: "D_YesNoStatus" },
          },
          // 账号状态
          {
            type: "dict",
            prop: "account_status",
            attrs: { name: "D_AccountStatus" },
          },
          // 公司是否认证
          {
            type: "dict",
            prop: "company_status",
            attrs: { name: "D_YesNoStatus" },
          },
          // 学校是否认证
          {
            type: "dict",
            prop: "school_status",
            attrs: { name: "D_YesNoStatus" },
          },
          // 学历：专科、本科、硕士、博士
          {
            type: "dict",
            prop: "school_certificate_level",
            attrs: { name: "D_EducationType" },
          },
          // 单身状态
          {
            type: "dict",
            prop: "single_type",
            attrs: { name: "D_MatrimonyStatus" },
          },
          // 职业名称
          {
            type: "helper",
            prop: "job_name",
            attrs: { typeKey: "pick", arr: allJobNames },
          },
          // 学校名称
          {
            type: "helper",
            prop: "school_name",
            attrs: { typeKey: "pick", arr: allSchoolNames },
          },
          // 公司名称
          {
            type: "helper",
            prop: "company_name",
            attrs: { typeKey: "pick", arr: allCompanyNames },
          },
          // 收入类型
          {
            type: "helper",
            prop: "income_type_name",
            attrs: { typeKey: "pick", arr: allIncomeTypes },
          },
          {
            type: "string",
            prop: "produce",
            attrs: { typeKey: "ctitle", min: 3, max: 200 },
          },
          {
            user_data: ({ context }: CommonObj) =>
              JSON.parse(JSON.stringify(context.currentContext)),
          },
          // 标签
          {
            tags({ context }: CommonObj) {
              const { id } = context.currentContext;
              const allTags = [
                { label: "用户标签1", value: 1 },
                { label: "用户标签2", value: 2 },
                { label: "用户标签3", value: 3 },
                { label: "用户标签4", value: 4 },
              ];
              return allTags.slice(id % 4);
            },
          },
          "enableStatus", // 启用禁用状态
          "createUpdate",
          "remark",
        ],
        42
      );
    },
  },
  // 题目（问答题）
  topics: {
    name: "题目",
    getList() {
      return getMockList(
        [
          "id",
          {
            prop: "title",
            attrs: { typeKey: "ctitle", min: 3, max: 30 },
          },
          "content:content:题目（问答题）内容",
          "amount:view_num:浏览量",
          "age:comment_num:评论数量",
          "age:praise_num:点赞数量",
          "age:dislike_num:点踩数量",
          "age:collect_num:收藏数量",
          "age:share_num:分享数量",
          "age:report_num:举报数量",
          "score",
          "enableStatus",
          "createUpdate",
          // 是否点赞
          {
            type: "dict",
            prop: "is_praise",
            attrs: { name: "D_YesNoStatus" },
          },
          // 是否点踩
          {
            type: "dict",
            prop: "is_dislike",
            attrs: { name: "D_YesNoStatus" },
          },
          // 用户id
          {
            prop: "user_id",
            remark: "用户id",
            attrs: { min: 1, max: 6 },
          },
          // 用户信息
          {
            user_info({ context }: CommonObj) {
              const { id } = context.currentContext;
              return { id: id % 6 || 1, nickname: "用户昵称mock" };
            },
          },
          // 推荐
          {
            recommend_list({ context }: CommonObj) {
              const { id } = context.currentContext;
              return Array(id % 10)
                .fill("")
                .map(() => ({}));
            },
          },
          // 标签
          {
            tags({ context }: CommonObj) {
              const { id } = context.currentContext;
              const allTags = [
                { label: "题目标签1", value: 1 },
                { label: "题目标签2", value: 2 },
                { label: "题目标签3", value: 3 },
                { label: "题目标签4", value: 4 },
              ];
              return allTags.slice(id % 4);
            },
          },
        ],
        42
      );
    },
  },
  // 笔记（文章）表
  note: {
    name: "笔记",
    getList() {
      return getMockList(
        [
          "id",
          {
            prop: "title",
            attrs: { typeKey: "ctitle", min: 3, max: 30 },
          },
          "content:content:笔记（文章）内容",
          "amount:view_num:浏览量",
          "age:comment_num:评论数量",
          "age:praise_num:点赞数量",
          "age:dislike_num:点踩数量",
          "age:collect_num:收藏数量",
          "age:share_num:分享数量",
          "age:report_num:举报数量",
          "score",
          "enableStatus",
          "createUpdate",
          // 是否点赞
          {
            type: "dict",
            prop: "is_praise",
            attrs: { name: "D_YesNoStatus" },
          },
          // 是否点踩
          {
            type: "dict",
            prop: "is_dislike",
            attrs: { name: "D_YesNoStatus" },
          },
          // 广告
          {
            adv_info({ context }: CommonObj) {
              const { id } = context.currentContext;
              const n = id % 2;
              if (!n) return null;
              return { src: "", url: "" };
            },
          },
          // 推荐
          {
            recommend_list({ context }: CommonObj) {
              const { id } = context.currentContext;
              return Array(id % 10)
                .fill("")
                .map(() => ({}));
            },
          },
          // 用户id
          {
            prop: "user_id",
            remark: "用户id",
            attrs: { min: 1, max: 6 },
          },
          // 用户信息
          {
            user_info({ context }: CommonObj) {
              const { id } = context.currentContext;
              return { id: id % 6 || 1, nickname: "用户昵称mock" };
            },
          },
          // 标签
          {
            tags({ context }: CommonObj) {
              const { id } = context.currentContext;
              const allTags = [
                { label: "文章标签1", value: 1 },
                { label: "文章标签2", value: 2 },
                { label: "文章标签3", value: 3 },
                { label: "文章标签4", value: 4 },
              ];
              return allTags.slice(id % 4);
            },
          },
        ],
        42
      );
    },
  },
  // 角色表
  role: {
    name: "角色",
    getList() {
      const roles = getDictValues("D_RoleType");
      const total = roles.length;
      return getMockList(
        [
          "id",
          {
            role: ({ context }: CommonObj) =>
              roles[context.currentContext.id - 1],
            role_text: ({ context }: CommonObj) =>
              getDictLabel("D_RoleType", context.currentContext.role),
          },
          "enableStatus",
          "createUpdate",
          "remark",
        ],
        total
      );
    },
  },
  // 职业
  job: {
    name: "职业",
    type: "options",
    getList() {
      return allJob;
    },
  },
  // 公司
  company: {
    name: "公司",
    type: "options",
    getList() {
      // const beforePrefix = () => Random.province();
      // const afterPrefix = ["股份有限公司", "科技公司", "有限公司", "公司"];
      // return getMockList(
      //   [
      //     "id",
      //     {
      //       full_name: () => `${beforePrefix().replace(/[省市]/g, "")}${Random.ctitle(3, 8)}${Random.pick(afterPrefix)}`,
      //       short_name: ({ context }: CommonObj) => Random.cword(context.currentContext.full_name, 1, 5),
      //     },
      //   ],
      //   100
      // );
      return allCompany;
    },
  },
  // 学校
  school: {
    name: "学校",
    type: "options",
    getList() {
      return allSchool;
    },
  },
  // 部门表
  department: {
    name: "部门",
    type: "cascader",
    getList() {
      return allDepartment;
    },
  },
  // 行政地址
  region: {
    name: "行政地址",
    type: "cascader",
    getList() {
      return allRegions;
    },
  },
  // 菜单
  menu: {
    name: "菜单",
    type: "cascader",
    getList() {
      return getNavsTree(allNavs);
    },
  },
};
